﻿Imports Control.clases
Public Class Marcas
    Inherits System.Web.UI.Page

    Dim marca As New clsMarca

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim dt As Data.DataTable
        If Not IsPostBack Then
            'Cargar Tabla
            dt = marca.listarMarca
            GridView1.DataSource = dt
            GridView1.PageIndex = 0
            GridView1.DataBind()
        End If
    End Sub

    Protected Sub GridView1_PageIndexChanging(sender As Object, e As GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        Dim dt As New Data.DataTable
        dt = marca.listarMarca
        GridView1.DataSource = dt
        GridView1.PageIndex = e.NewPageIndex
        GridView1.DataBind()
        GridView1.SelectedIndex = 0
        Dim codigo As String = GridView1.Rows(0).Cells(1).Text
        cargarObjeto(codigo)
    End Sub

    Private Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "Select" Then
            Dim i As Integer = Integer.Parse(e.CommandArgument.ToString())
            Dim codigo As String = GridView1.Rows(i).Cells(1).Text
            cargarObjeto(codigo)
            btnModificar.Enabled = True
        End If
    End Sub

    Private Sub cargarObjeto(ByVal codigo As String)
        Dim dt As New Data.DataTable
        dt = marca.buscarMarca(codigo)
        If dt.Rows.Count > 0 Then
            lblCodigo.Text = dt.Rows(0)("id_marca").ToString()
            txtDescripcion.Text = dt.Rows(0)("marca_descripcion").ToString()
            txtSiglas.Text = dt.Rows(0)("marca_sigla").ToString()
            txtPaginaWeb.Text = dt.Rows(0)("marca_pagina_web").ToString()
        End If
    End Sub

    Private Sub ConfigForm(ByVal opt_menu As String, ByVal bloqueo As Boolean)
        txtDescripcion.Enabled = Not bloqueo
        txtSiglas.Enabled = Not bloqueo
        txtPaginaWeb.Enabled = Not bloqueo

        btnNuevo.Enabled = bloqueo '//BOTON NUEVO
        btnModificar.Enabled = bloqueo '//BOTON MODIFICAR
        btnGrabar.Enabled = Not bloqueo '//BOTON GRABAR
        btnCancelar.Enabled = Not bloqueo '//BOTON CANCELAR
        btnEliminar.Enabled = bloqueo '//BOTON ELIMINAR
        btnGrabar.CommandArgument = opt_menu '//ARGUMENTO A BOTON GRABAR
    End Sub

    Private Sub CleanForm()
        txtDescripcion.Text = ""
        txtPaginaWeb.Text = ""
        txtSiglas.Text = ""
    End Sub

    Protected Sub btnNuevo_Click(sender As Object, e As EventArgs) Handles btnNuevo.Click
        ConfigForm("NEW", False)
        CleanForm()
    End Sub

    Protected Sub btnGrabar_Click(sender As Object, e As EventArgs) Handles btnGrabar.Click
        Dim oNuevo As New clsMarca()
        If btnGrabar.CommandArgument = "NEW" Then
            oNuevo.agregarMarca(txtDescripcion.Text, txtSiglas.Text, txtPaginaWeb.Text)
        ElseIf btnGrabar.CommandArgument = "EDIT" Then
            oNuevo.actualizarMarca(lblCodigo.Text, txtDescripcion.Text, txtSiglas.Text, txtPaginaWeb.Text)
        End If
        ConfigForm("SELECT", True)
        Dim dt As Data.DataTable
        dt = oNuevo.listarMarca
        GridView1.DataSource = dt
        If btnGrabar.CommandArgument = "NEW" Then
            GridView1.DataBind()
            GridView1.PageIndex = GridView1.PageCount - 1
            GridView1.Rows(GridView1.Rows.Count - 1).RowState = DataControlRowState.Selected
            Dim codigo As String = GridView1.Rows(dt.Rows.Count - 1).Cells(1).Text
            cargarObjeto(codigo)
        ElseIf btnGrabar.CommandArgument = "EDIT" Then
            Dim i_fila As Integer = GridView1.SelectedRow.RowIndex
            GridView1.Rows(i_fila).RowState = DataControlRowState.Selected
            Dim codigo As String = GridView1.Rows(i_fila).Cells(1).Text
            cargarObjeto(codigo)
        End If
        GridView1.DataBind()
    End Sub

    Protected Sub btnModificar_Click(sender As Object, e As EventArgs) Handles btnModificar.Click
        ConfigForm("EDIT", False)
    End Sub

    Protected Sub btnCancelar_Click(sender As Object, e As EventArgs) Handles btnCancelar.Click
        Dim dt As Data.DataTable
        ConfigForm("SELECT", True)
        dt = marca.listarMarca
        GridView1.DataSource = dt
        GridView1.DataBind()
        If dt.Rows.Count > 0 Then
        Else
            CleanForm()
        End If
    End Sub

    Protected Sub btnEliminar_Click(sender As Object, e As EventArgs) Handles btnEliminar.Click
        Try
            If GridView1.Rows.Count > 0 Then
                Dim page_ind As Integer = 0
                Dim i_fila As Integer = GridView1.SelectedRow.RowIndex
                Dim codigo As String = GridView1.Rows(i_fila).Cells(1).Text

                marca.eliminarMarca(lblCodigo.Text)

                If GridView1.Rows.Count = 0 Or GridView1.PageCount = 0 Then
                    page_ind = 0
                Else
                    page_ind = GridView1.PageIndex
                End If

                Dim dt As Data.DataTable
                dt = marca.listarMarca()
                GridView1.DataSource = dt
                GridView1.PageIndex = page_ind
                GridView1.DataBind()
                CleanForm()
            End If
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub
End Class